#include "level.h"
#include "ball.h"
#include "util.h"

Level::Level()
{
	numBlocks = 6;

	blocks[0]=new Obstacle(3,4,3,4);  //no x, w sum over 50.  no y, h > 37
	blocks[1]=new Obstacle(16,4,12,4); 
	blocks[2]=new Obstacle(10,12,4,4);
	blocks[3]=new Obstacle(20,30,1,5);
	blocks[4]=new Obstacle(8,24,15,1);
	blocks[5]=new Obstacle(40,20,2,2,true);
}

Level::Level(std::string levelName)
{
	int obstacles=hge->Ini_GetInt(levelName.c_str(),"boxes",0);
	numLevels=hge->Ini_GetInt(levelName.c_str(), "levels", 0);
	music=hge->Ini_GetString(levelName.c_str(), "music", 0);

	numBlocks=obstacles;
	float x,y,w,h;

	for (int i=0;i<obstacles;i++){
		std::string box_x = "box"+num2str(i)+"x";
		std::string box_y = "box"+num2str(i)+"y";
		std::string box_w = "box"+num2str(i)+"w";
		std::string box_h = "box"+num2str(i)+"h";
		x=hge->Ini_GetFloat(levelName.c_str(),box_x.c_str(),0);
		y=hge->Ini_GetFloat(levelName.c_str(),box_y.c_str(),0);
		w=hge->Ini_GetFloat(levelName.c_str(),box_w.c_str(),0);
		h=hge->Ini_GetFloat(levelName.c_str(),box_h.c_str(),0);
		blocks[i]=new Obstacle(x,y,w,h);
	}
	x=hge->Ini_GetFloat(levelName.c_str(),"winbox_x",0);
	y=hge->Ini_GetFloat(levelName.c_str(),"winbox_y",0);
	w=hge->Ini_GetFloat(levelName.c_str(),"winbox_w",0);
	h=hge->Ini_GetFloat(levelName.c_str(),"winbox_h",0);
	blocks[obstacles-1] = new Obstacle(x,y,w,h,true);
}

Level::~Level()
{
	for(int i=0; i<numBlocks; i++)
		delete blocks[i];
}

void Level::Render()
{
	for(int i=0; i<numBlocks; i++)
		blocks[i]->Render();
}

int Level::Collide(Ball *ball)
{
	int collided=0;
	for(int i=0; i<numBlocks; i++)
		collided += blocks[i]->Collide(ball);
	if (collided>0) return collided;
	return -1;
}